package com.h3bpm.kumhosunny.crm.controller;/**
 * @Auther: Administrator
 * @Date: 2019/11/4 22:56
 * @Description:
 */

import OThinker.H3.Controller.ControllerBase;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName:
 * @Description: 描述  手机端crm列表判断文件是否存在
 *
 * @author WJQ
 * @version V1.0
 * @Date
 */
@Controller
@RequestMapping("/Portal/CRM/Mobile")
public class MobilePicController extends ControllerBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(MobilePicController.class);

    @Override
    public String getFunctionCode() {
        return null;
    }

    public String fourZeoreFour = "HTTP/1.1 404";



    /****
     * 功能描述: 判断对应编码的图片是否存在
     *
     * @Param: [instanceId]
     * @Return:
     * @Author: wjq
     * @Date: 2019/11/2 14:09
     */
    @RequestMapping(
            value = {"/testPicture"}
    )
    @ResponseBody
    public JSONObject testPicture(String code) throws  Exception {
        LOGGER.info("验证图片是否存在接口==========code==" + code);
        JSONObject result = new JSONObject();
        String filePth = "/Portal/kumhosunny/mobile/" + code + ".png";
        URL url = new URL("http://" + request.getServerName() + ":" + request.getServerPort() + filePth);
        HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
        String message = urlcon.getHeaderField(0);
        if (StringUtils.hasText(message) && message.startsWith(fourZeoreFour)) {
            // 文件不存在
            result.put("code", 200);
            result.put("errorMsg", "请求成功");
            result.put("data", false);
            result.put("address", null);
        } else {
            result.put("code", 200);
            result.put("errorMsg", "请求成功");
            result.put("data", true);
            result.put("address", filePth);

            result.put("msg", "请求成功");
            result.put("code", "200");
            result.put("data", false);
            result.put("address", null);
            LOGGER.info("验证图片是否存在接口==========address==" + result.getString("address"));

            return result;
        }
        return result;
    }
}